<template>
	<main class="cart_box background_f0">
		<div class="line_height40 overflow_hidden padding_lf10_rg10 border_bot_dcdcdc">
			<div class="fr">
				<span>需支付：<strong class="color_main_red">{{totalPrice}}元</strong></span>
			</div>
		</div>
		<div class="line_height40 overflow_hidden padding_lf10_rg10 border_bot_dcdcdc background_fff">
			<div class="fl">
				<img src="../assets/images/weixinpay_icon.png" /><span>微信支付</span>
			</div>
		</div>
		<div @click="WXOrderPay()" class="btn_bot bottom66">
			支付
		</div>
	</main>
</template>
<script>
	import { Switch } from 'mint-ui';
	import { Toast } from 'mint-ui';
	export default {
		created() {
			let _this = this
			_this.OrderId = _this.$route.query.OrderId
			_this.totalPrice = _this.$route.query.totalPrice
			_this.getWX();
		},
		data() {
			return {
				root: this.root,
				OrderId: '',
				totalPrice: 0
			}
		},
		methods: {
			WXOrderPay() {
				let _this = this
//				alert('点击了支付按钮')
				console.log('点击了支付按钮')
				_this.$http.post('/client/OrderPays', {
					OrderId: _this.OrderId,
					PayType: 2
				}, localStorage.Authorization, res => {
//					alert('调用/client/OrderPays回调函数的res')
//					alert(JSON.stringify(res))
					console.log('调用/client/OrderPays回调函数的res', JSON.stringify(res))
					if(true) {
						WeixinJSBridge.invoke(
							'getBrandWCPayRequest', {
								"appId": res.data.appId,
								"nonceStr": res.data.nonceStr,
								"package": res.data.package,
								"paySign": res.data.paySign,
								"signType": res.data.signType,
								"timeStamp": res.data.timeStamp
							},
							function(res) {
//								alert('微信的WeixinJSBridge.invoke方法的回调')
//								alert(JSON.stringify(res))
								console.log('微信的WeixinJSBridge.invoke方法的回调res', res)
								if(res.err_msg == "get_brand_wcpay_request:ok") {
//									alert('ok res')
									
									_this.$http.get('/client/OrderPays/queryOrder', {
										OrderId: _this.OrderId,
									}, localStorage.Authorization, res => {
										if(res.code == 200) {
											Toast(res.data)
											_this.$router.push({path: '/wx/cart'})
											_this.$router.go(-1)
										}else{
											Toast(res.error)
											_this.$router.push({path: '/wx/cart'})
											_this.$router.go(-1)
										}

									})

								}
							}
						);
					}
				})
			},
			getWX() {
				let _this = this
				var currentUrl = location.href.split('#')[0]
				_this.$http.get('/client/wxConfig', {
					configUrl: currentUrl
				}, localStorage.Authorization, res => {
					if(res.code === 200) {
						wx.config(res.data);
					}
				})
			},

		},
		watch: {

		}
	}
</script>
<style scoped="scoped">

</style>